home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / hplip / ui4 / devmgr5.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-10-28  |  64KB  |  1,745 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import time
  6. import os
  7. import gzip
  8. import select
  9. import struct
  10. from base.g import *
  11. from base import device, utils, pml, maint, models, pkit
  12. from prnt import cups
  13. from base.codes import *
  14. from ui_utils import *
  15. import hpmudext
  16. from PyQt4.QtCore import *
  17. from PyQt4.QtGui import *
  18.  
  19. try:
  20.     import dbus
  21.     from dbus.mainloop.qt import DBusQtMainLoop
  22.     from dbus import lowlevel
  23. except ImportError:
  24.     log.error('Unable to load DBus libraries. Please check your installation and try again.')
  25.     sys.exit(1)
  26.  
  27. import warnings
  28. warnings.simplefilter('ignore', DeprecationWarning)
  29. from devmgr5_base import Ui_MainWindow
  30. from faxsetupdialog import FaxSetupDialog
  31. from plugindialog import PluginDialog
  32. from firmwaredialog import FirmwareDialog
  33. from aligndialog import AlignDialog
  34. from printdialog import PrintDialog
  35. from makecopiesdialog import MakeCopiesDialog
  36. from sendfaxdialog import SendFaxDialog
  37. from fabwindow import FABWindow
  38. from devicesetupdialog import DeviceSetupDialog
  39. from printtestpagedialog import PrintTestPageDialog
  40. from infodialog import InfoDialog
  41. from cleandialog import CleanDialog
  42. from colorcaldialog import ColorCalDialog
  43. from linefeedcaldialog import LineFeedCalDialog
  44. from pqdiagdialog import PQDiagDialog
  45. from nodevicesdialog import NoDevicesDialog
  46. from aboutdialog import AboutDialog
  47. from settingsdialog import SettingsDialog
  48. from printsettingstoolbox import PrintSettingsToolbox
  49. MIN_AUTO_REFRESH_RATE = 5
  50. MAX_AUTO_REFRESH_RATE = 60
  51. DEF_AUTO_REFRESH_RATE = 30
  52. device_list = { }
  53. model_obj = models.ModelData()
  54.  
  55. class FuncViewItem(QListWidgetItem):
  56.     
  57.     def __init__(self, parent, text, pixmap, tooltip_text, cmd):
  58.         QListWidgetItem.__init__(self, QIcon(pixmap), text, parent)
  59.         self.tooltip_text = tooltip_text
  60.         self.cmd = cmd
  61.  
  62.  
  63.  
  64. class DeviceViewItem(QListWidgetItem):
  65.     
  66.     def __init__(self, parent, text, pixmap, device_uri, is_avail = True):
  67.         QListWidgetItem.__init__(self, QIcon(pixmap), text, parent)
  68.         self.device_uri = device_uri
  69.         self.is_avail = is_avail
  70.         self.setTextAlignment(Qt.AlignHCenter)
  71.  
  72.  
  73.  
  74. class PluginInstall(QObject):
  75.     
  76.     def __init__(self, parent, plugin_type, plugin_installed):
  77.         self.parent = parent
  78.         self.plugin_type = plugin_type
  79.         self.plugin_installed = plugin_installed
  80.  
  81.     
  82.     def exec_(self):
  83.         install_plugin = True
  84.         if self.plugin_installed:
  85.             install_plugin = QMessageBox.warning(self.parent, self.parent.windowTitle(), self._PluginInstall__tr('<b>The HPLIP plugin is already installed.</b><p>Do you want to continue and re-install it?'), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton) == QMessageBox.Yes
  86.         
  87.         if install_plugin:
  88.             (ok, sudo_ok) = pkit.run_plugin_command(self.plugin_type == PLUGIN_REQUIRED, self.parent.cur_device.mq['plugin-reason'])
  89.             if not sudo_ok:
  90.                 QMessageBox.critical(self.parent, self.parent.windowTitle(), self._PluginInstall__tr('<b>Unable to find an appropriate su/sudo utility to run hp-plugin.</b><p>Install kdesu, gnomesu, or gksu.</p>'), QMessageBox.Ok, QMessageBox.NoButton, QMessageBox.NoButton)
  91.             
  92.         
  93.  
  94.     
  95.     def __tr(self, s, c = None):
  96.         return qApp.translate('DevMgr5', s, c)
  97.  
  98.  
  99.  
  100. class DevMgr5(QMainWindow, Ui_MainWindow):
  101.     
  102.     def __init__(self, toolbox_version, initial_device_uri = None, dbus_loop = None, parent = None, name = None, fl = 0):
  103.         QMainWindow.__init__(self, parent)
  104.         log.debug('Initializing toolbox UI (Qt4)...')
  105.         log.debug('HPLIP Version: %s' % prop.installed_version)
  106.         self.setupUi(self)
  107.         self.toolbox_version = toolbox_version
  108.         self.initial_device_uri = initial_device_uri
  109.         self.device_vars = { }
  110.         self.num_devices = 0
  111.         self.cur_device = None
  112.         self.cur_printer = None
  113.         self.updating = False
  114.         self.init_failed = False
  115.         self.service = None
  116.         self.user_settings = UserSettings()
  117.         self.user_settings.load()
  118.         self.user_settings.debug()
  119.         self.cur_device_uri = self.user_settings.last_used_device_uri
  120.         self.initDBus()
  121.         self.initPixmaps()
  122.         self.initMisc()
  123.         self.initUI()
  124.         cups.setPasswordCallback(showPasswordUI)
  125.         if not prop.doc_build:
  126.             self.ContentsAction.setEnabled(False)
  127.         
  128.         self.allow_auto_refresh = True
  129.         QTimer.singleShot(0, self.initalUpdate)
  130.  
  131.     
  132.     def initDBus(self):
  133.         self.dbus_loop = DBusQtMainLoop(set_as_default = True)
  134.         (self.dbus_avail, self.service, self.session_bus) = device.init_dbus(self.dbus_loop)
  135.         if not self.dbus_avail:
  136.             log.error('dBus initialization error. Exiting.')
  137.             self.init_failed = True
  138.             return None
  139.         self.session_bus.add_signal_receiver(self.handleSessionSignal, sender_keyword = 'sender', destination_keyword = 'dest', interface_keyword = 'interface', member_keyword = 'member', path_keyword = 'path')
  140.  
  141.     
  142.     def initPixmaps(self):
  143.         self.func_icons_cached = False
  144.         self.func_icons = { }
  145.         self.device_icons = { }
  146.         self.setWindowIcon(QIcon(load_pixmap('hp_logo', '128x128')))
  147.         self.fax_icon = load_pixmap('fax2', 'other')
  148.  
  149.     
  150.     def initUI(self):
  151.         self.DeviceList.setSortingEnabled(True)
  152.         self.DeviceList.setContextMenuPolicy(Qt.CustomContextMenu)
  153.         self.setDeviceListViewMode(QListView.IconMode)
  154.         self.connect(self.ViewAsIconsAction, (SIGNAL('triggered()'),), (lambda : self.setDeviceListViewMode(QListView.IconMode)))
  155.         self.connect(self.ViewAsListAction, (SIGNAL('triggered()'),), (lambda : self.setDeviceListViewMode(QListView.ListMode)))
  156.         self.connect(self.DeviceList, SIGNAL('customContextMenuRequested(const QPoint &)'), self.DeviceList_customContextMenuRequested)
  157.         self.DeviceRefreshAction.setIcon(QIcon(load_pixmap('refresh1', '16x16')))
  158.         self.connect(self.DeviceRefreshAction, SIGNAL('triggered()'), self.DeviceRefreshAction_activated)
  159.         self.RefreshAllAction.setIcon(QIcon(load_pixmap('refresh', '16x16')))
  160.         self.connect(self.RefreshAllAction, SIGNAL('triggered()'), self.RefreshAllAction_activated)
  161.         self.SetupDeviceAction.setIcon(QIcon(load_pixmap('list_add', '16x16')))
  162.         self.connect(self.SetupDeviceAction, SIGNAL('triggered()'), self.SetupDeviceAction_activated)
  163.         self.RemoveDeviceAction.setIcon(QIcon(load_pixmap('list_remove', '16x16')))
  164.         self.connect(self.RemoveDeviceAction, SIGNAL('triggered()'), self.RemoveDeviceAction_activated)
  165.         self.PreferencesAction.setIcon(QIcon(load_pixmap('settings', '16x16')))
  166.         self.connect(self.PreferencesAction, SIGNAL('triggered()'), self.PreferencesAction_activated)
  167.         self.ContentsAction.setIcon(QIcon(load_pixmap('help', '16x16')))
  168.         self.connect(self.ContentsAction, SIGNAL('triggered()'), self.helpContents)
  169.         self.QuitAction.setIcon(QIcon(load_pixmap('quit', '16x16')))
  170.         self.connect(self.QuitAction, SIGNAL('triggered()'), self.quit)
  171.         self.connect(self.AboutAction, SIGNAL('triggered()'), self.helpAbout)
  172.         self.connect(self.PrintControlPrinterNameCombo, SIGNAL('activated(const QString &)'), self.PrintControlPrinterNameCombo_activated)
  173.         self.connect(self.PrintSettingsPrinterNameCombo, SIGNAL('activated(const QString &)'), self.PrintSettingsPrinterNameCombo_activated)
  174.         self.initActionsTab()
  175.         self.initStatusTab()
  176.         self.initSuppliesTab()
  177.         self.initPrintSettingsTab()
  178.         self.initPrintControlTab()
  179.         self.connect(self.Tabs, SIGNAL('currentChanged(int)'), self.Tabs_currentChanged)
  180.         self.splitter.setSizes([
  181.             80,
  182.             600])
  183.         self.DeviceList.setIconSize(QSize(60, 60))
  184.         self.connect(self.DeviceList, SIGNAL('currentItemChanged(QListWidgetItem * ,QListWidgetItem *)'), self.DeviceList_currentChanged)
  185.  
  186.     
  187.     def initMisc(self):
  188.         self.TabIndex = {
  189.             0: self.updateActionsTab,
  190.             1: self.updateStatusTab,
  191.             2: self.updateSuppliesTab,
  192.             3: self.updatePrintSettingsTab,
  193.             4: self.updatePrintControlTab }
  194.         self.docs = 'http://hplip.sf.net'
  195.         if prop.doc_build:
  196.             g = os.path.join(sys_conf.get('dirs', 'doc'), 'index.html')
  197.             if os.path.exists(g):
  198.                 self.docs = 'file://%s' % g
  199.             
  200.         
  201.         self.support = 'https://launchpad.net/hplip'
  202.  
  203.     
  204.     def initalUpdate(self):
  205.         if self.init_failed:
  206.             self.close()
  207.             return None
  208.         self.rescanDevices()
  209.         cont = True
  210.         if self.initial_device_uri is not None:
  211.             if not self.activateDevice(self.initial_device_uri):
  212.                 log.error('Device %s not found' % self.initial_device_uri)
  213.                 cont = False
  214.             
  215.         
  216.         if self.cur_printer:
  217.             self.getPrinterState()
  218.             if self.printer_state == cups.IPP_PRINTER_STATE_STOPPED:
  219.                 self.cur_device.sendEvent(EVENT_PRINTER_QUEUE_STOPPED, self.cur_printer)
  220.             
  221.             if not self.printer_accepting:
  222.                 self.cur_device.sendEvent(EVENT_PRINTER_QUEUE_REJECTING_JOBS, self.cur_printer)
  223.             
  224.         
  225.  
  226.     
  227.     def activateDevice(self, device_uri):
  228.         log.debug(log.bold('Activate: %s %s %s' % ('********************', device_uri, '********************')))
  229.         index = 0
  230.         d = self.DeviceList.item(index)
  231.         found = False
  232.         while d is not None:
  233.             if d.device_uri == device_uri:
  234.                 found = True
  235.                 self.DeviceList.setSelected(d, True)
  236.                 self.DeviceList.setCurrentItem(d)
  237.                 break
  238.             
  239.             index += 1
  240.             d = self.DeviceList.item(index)
  241.         return found
  242.  
  243.     
  244.     def handleSessionSignal(self, *args, **kwds):
  245.         if kwds['interface'] == 'com.hplip.Toolbox' and kwds['member'] == 'Event':
  246.             log.debug('Handling event...')
  247.             event = device.Event(*args[:6])
  248.             event.debug()
  249.             if event.event_code < EVENT_MIN_USER_EVENT:
  250.                 pass
  251.             elif event.event_code == EVENT_DEVICE_UPDATE_REPLY:
  252.                 log.debug('EVENT_DEVICE_UPDATE_REPLY (%s)' % event.device_uri)
  253.                 dev = self.findDeviceByURI(event.device_uri)
  254.                 if dev is not None:
  255.                     
  256.                     try:
  257.                         self.service.GetStatus(event.device_uri, reply_handler = self.handleStatusReply, error_handler = self.handleStatusError)
  258.                     except dbus.exceptions.DBusException:
  259.                         e = None
  260.                         log.error('dbus call to GetStatus() failed.')
  261.                     except:
  262.                         None<EXCEPTION MATCH>dbus.exceptions.DBusException
  263.                     
  264.  
  265.                 None<EXCEPTION MATCH>dbus.exceptions.DBusException
  266.             elif event.event_code == EVENT_USER_CONFIGURATION_CHANGED:
  267.                 log.debug('EVENT_USER_CONFIGURATION_CHANGED')
  268.                 self.user_settings.load()
  269.             elif event.event_code == EVENT_HISTORY_UPDATE:
  270.                 log.debug('EVENT_HISTORY_UPDATE (%s)' % event.device_uri)
  271.                 dev = self.findDeviceByURI(event.device_uri)
  272.                 if dev is not None:
  273.                     self.updateHistory(dev)
  274.                 
  275.             elif event.event_code == EVENT_SYSTEMTRAY_EXIT:
  276.                 log.debug('EVENT_SYSTEMTRAY_EXIT')
  277.                 log.error('HPLIP Status Service was closed. HPLIP Device Manager will now exit.')
  278.                 self.close()
  279.             elif event.event_code == EVENT_RAISE_DEVICE_MANAGER:
  280.                 log.debug('EVENT_RAISE_DEVICE_MANAGER')
  281.                 self.showNormal()
  282.                 self.setWindowState(self.windowState() & ~(Qt.WindowMinimized) | Qt.WindowActive)
  283.                 self.raise_()
  284.             elif event.event_code in (EVENT_DEVICE_START_POLLING, EVENT_DEVICE_STOP_POLLING, EVENT_POLLING_REQUEST):
  285.                 pass
  286.             else:
  287.                 log.error('Unhandled event: %d' % event.event_code)
  288.         
  289.  
  290.     
  291.     def handleStatusReply(self, device_uri, data):
  292.         dev = self.findDeviceByURI(device_uri)
  293.         if dev is not None:
  294.             t = { }
  295.             for key in data:
  296.                 value = model_obj.convert_data(str(key), str(data[key]))
  297.                 t.setdefault(key, value)
  298.             
  299.             dev.dq = t.copy()
  300.             for d in dev.dq:
  301.                 dev.__dict__[d.replace('-', '_')] = dev.dq[d]
  302.             
  303.             self.updateDevice(dev)
  304.         
  305.  
  306.     
  307.     def handleStatusError(self, e):
  308.         log.error(str(e))
  309.  
  310.     
  311.     def updateHistory(self, dev = None):
  312.         if dev is None:
  313.             dev = self.cur_device
  314.         
  315.         
  316.         try:
  317.             self.service.GetHistory(dev.device_uri, reply_handler = self.handleHistoryReply, error_handler = self.handleHistoryError)
  318.         except dbus.exceptions.DBusException:
  319.             e = None
  320.             log.error('dbus call to GetHistory() failed.')
  321.  
  322.  
  323.     
  324.     def handleHistoryReply(self, device_uri, history):
  325.         dev = self.findDeviceByURI(device_uri)
  326.         if dev is not None:
  327.             result = []
  328.             history.reverse()
  329.             for h in history:
  330.                 result.append(device.Event(*tuple(h)))
  331.             
  332.             
  333.             try:
  334.                 self.error_code = result[0].event_code
  335.             except IndexError:
  336.                 self.error_code = STATUS_UNKNOWN
  337.  
  338.             dev.error_state = STATUS_TO_ERROR_STATE_MAP.get(self.error_code, ERROR_STATE_CLEAR)
  339.             dev.hist = result
  340.             self.updateDevice(dev)
  341.         
  342.  
  343.     
  344.     def handleHistoryError(self, e):
  345.         log.error(str(e))
  346.  
  347.     
  348.     def sendMessage(self, device_uri, printer_name, event_code, username = prop.username, job_id = 0, title = ''):
  349.         device.Event(device_uri, printer_name, event_code, username, job_id, title).send_via_dbus(self.session_bus)
  350.  
  351.     
  352.     def timedRefresh(self):
  353.         if not (self.updating) and self.user_settings.auto_refresh and self.allow_auto_refresh:
  354.             log.debug('Refresh timer...')
  355.             self.cleanupChildren()
  356.             if self.user_settings.auto_refresh_type == 0:
  357.                 self.requestDeviceUpdate()
  358.             else:
  359.                 self.rescanDevices()
  360.         
  361.  
  362.     
  363.     def Tabs_currentChanged(self, tab = 0):
  364.         ''' Called when the active tab changes.
  365.             Update newly displayed tab.
  366.         '''
  367.         if self.cur_device is not None:
  368.             self.TabIndex[tab]()
  369.         
  370.  
  371.     
  372.     def updateAllTabs(self):
  373.         for tab in self.TabIndex:
  374.             self.TabIndex[tab]()
  375.         
  376.  
  377.     
  378.     def updateCurrentTab(self):
  379.         log.debug('updateCurrentTab()')
  380.         self.TabIndex[self.Tabs.currentIndex()]()
  381.  
  382.     
  383.     def DeviceRefreshAction_activated(self):
  384.         self.DeviceRefreshAction.setEnabled(False)
  385.         self.requestDeviceUpdate()
  386.         self.DeviceRefreshAction.setEnabled(True)
  387.  
  388.     
  389.     def RefreshAllAction_activated(self):
  390.         self.rescanDevices()
  391.  
  392.     
  393.     def setDeviceListViewMode(self, mode):
  394.         if mode == QListView.ListMode:
  395.             self.DeviceList.setViewMode(QListView.ListMode)
  396.             self.ViewAsListAction.setEnabled(False)
  397.             self.ViewAsIconsAction.setEnabled(True)
  398.         else:
  399.             self.DeviceList.setViewMode(QListView.IconMode)
  400.             self.ViewAsListAction.setEnabled(True)
  401.             self.ViewAsIconsAction.setEnabled(False)
  402.  
  403.     
  404.     def createDeviceIcon(self, dev = None):
  405.         if dev is None:
  406.             dev = self.cur_device
  407.         
  408.         
  409.         try:
  410.             dev.icon
  411.         except AttributeError:
  412.             dev.icon = 'default_printer'
  413.  
  414.         
  415.         try:
  416.             self.device_icons[dev.icon]
  417.         except:
  418.             self.device_icons[dev.icon] = load_pixmap(dev.icon, 'devices')
  419.  
  420.         pix = self.device_icons[dev.icon]
  421.         w = pix.width()
  422.         h = pix.height()
  423.         error_state = dev.error_state
  424.         icon = QPixmap(w, h)
  425.         p = QPainter(icon)
  426.         p.eraseRect(0, 0, icon.width(), icon.height())
  427.         p.drawPixmap(0, 0, pix)
  428.         
  429.         try:
  430.             tech_type = dev.tech_type
  431.         except AttributeError:
  432.             tech_type = TECH_TYPE_NONE
  433.  
  434.         if dev.device_type == DEVICE_TYPE_FAX:
  435.             p.drawPixmap(w - self.fax_icon.width(), 0, self.fax_icon)
  436.         
  437.         if error_state != ERROR_STATE_CLEAR:
  438.             if tech_type in (TECH_TYPE_COLOR_INK, TECH_TYPE_MONO_INK):
  439.                 status_icon = getStatusOverlayIcon(error_state)[0]
  440.             else:
  441.                 status_icon = getStatusOverlayIcon(error_state)[1]
  442.             if status_icon is not None:
  443.                 p.drawPixmap(0, 0, status_icon)
  444.             
  445.         
  446.         p.end()
  447.         return icon
  448.  
  449.     
  450.     def refreshDeviceList(self):
  451.         log.debug('Rescanning device list...')
  452.         beginWaitCursor()
  453.         self.updating = True
  454.         self.setWindowTitle(self._DevMgr5__tr('Refreshing Device List - HP Device Manager'))
  455.         self.statusBar().showMessage(self._DevMgr5__tr('Refreshing device list...'))
  456.         self.cups_devices = device.getSupportedCUPSDevices([
  457.             'hp',
  458.             'hpfax'])
  459.         current = None
  460.         
  461.         try:
  462.             adds = []
  463.             for d in self.cups_devices:
  464.                 if d not in device_list:
  465.                     adds.append(d)
  466.                     continue
  467.             
  468.             log.debug('Adds: %s' % ','.join(adds))
  469.             removals = []
  470.             for d in device_list:
  471.                 if d not in self.cups_devices:
  472.                     removals.append(d)
  473.                     continue
  474.             
  475.             log.debug('Removals (1): %s' % ','.join(removals))
  476.             updates = []
  477.             for d in device_list:
  478.                 if d not in adds and d not in removals:
  479.                     updates.append(d)
  480.                     continue
  481.             
  482.             log.debug('Updates: %s' % ','.join(updates))
  483.             for d in adds:
  484.                 log.debug('adding: %s' % d)
  485.                 dev = device.Device(d, service = self.service, disable_dbus = False)
  486.                 if not dev.supported:
  487.                     log.debug('Unsupported model - removing device.')
  488.                     removals.append(d)
  489.                     continue
  490.                 
  491.                 icon = self.createDeviceIcon(dev)
  492.                 if dev.device_type == DEVICE_TYPE_FAX:
  493.                     DeviceViewItem(self.DeviceList, self._DevMgr5__tr('%1 (Fax)').arg(dev.model_ui), icon, d)
  494.                 elif dev.fax_type:
  495.                     DeviceViewItem(self.DeviceList, self._DevMgr5__tr('%1 (Printer)').arg(dev.model_ui), icon, d)
  496.                 else:
  497.                     DeviceViewItem(self.DeviceList, dev.model_ui, icon, d)
  498.                 device_list[d] = dev
  499.             
  500.             log.debug('Removals (2): %s' % ','.join(removals))
  501.             for d in removals:
  502.                 index = self.DeviceList.count() - 1
  503.                 item = self.DeviceList.item(index)
  504.                 log.debug('removing: %s' % d)
  505.                 
  506.                 try:
  507.                     del device_list[d]
  508.                 except KeyError:
  509.                     pass
  510.  
  511.                 while index >= 0 and item is not None:
  512.                     if item.device_uri == d:
  513.                         self.DeviceList.takeItem(index)
  514.                         break
  515.                     
  516.                     index -= 1
  517.                     item = self.DeviceList.item(index)
  518.                 qApp.processEvents()
  519.             
  520.             self.DeviceList.updateGeometry()
  521.             qApp.processEvents()
  522.             if len(device_list):
  523.                 for tab in self.TabIndex:
  524.                     self.Tabs.setTabEnabled(tab, True)
  525.                 
  526.                 if self.cur_device_uri:
  527.                     index = 0
  528.                     item = first_item = self.DeviceList.item(index)
  529.                     while item is not None:
  530.                         qApp.processEvents()
  531.                         if item.device_uri == self.cur_device_uri:
  532.                             current = item
  533.                             self.statusBar().showMessage(self.cur_device_uri)
  534.                             break
  535.                         
  536.                         index += 1
  537.                         item = self.DeviceList.item(index)
  538.                     self.cur_device = None
  539.                     self.cur_device_uri = ''
  540.                 
  541.                 if self.cur_device is None:
  542.                     i = self.DeviceList.item(0)
  543.                     if i is not None:
  544.                         self.cur_device_uri = i.device_uri
  545.                         self.cur_device = device_list[self.cur_device_uri]
  546.                         current = i
  547.                     
  548.                 
  549.                 self.updatePrinterCombos()
  550.                 if self.cur_device_uri:
  551.                     self.user_settings.last_used_device_uri = self.cur_device_uri
  552.                     self.user_settings.save()
  553.                 
  554.                 for d in updates + adds:
  555.                     if d not in removals:
  556.                         self.requestDeviceUpdate(device_list[d])
  557.                         continue
  558.                 
  559.             else:
  560.                 self.cur_device = None
  561.                 self.DeviceRefreshAction.setEnabled(False)
  562.                 self.RemoveDeviceAction.setEnabled(False)
  563.                 self.updating = False
  564.                 self.statusBar().showMessage(self._DevMgr5__tr('Press F6 to refresh.'))
  565.                 for tab in self.TabIndex:
  566.                     self.Tabs.setTabEnabled(tab, False)
  567.                 
  568.                 endWaitCursor()
  569.                 dlg = NoDevicesDialog(self)
  570.                 dlg.exec_()
  571.         finally:
  572.             self.updating = False
  573.             endWaitCursor()
  574.  
  575.         if current is not None:
  576.             self.DeviceList.setCurrentItem(current)
  577.         
  578.         self.DeviceRefreshAction.setEnabled(True)
  579.         if self.cur_device is not None:
  580.             self.RemoveDeviceAction.setEnabled(True)
  581.             self.statusBar().showMessage(self.cur_device_uri)
  582.             self.updateWindowTitle()
  583.         
  584.  
  585.     
  586.     def updateWindowTitle(self):
  587.         if self.cur_device.device_type == DEVICE_TYPE_FAX:
  588.             self.setWindowTitle(self._DevMgr5__tr('HP Device Manager - %1 (Fax)').arg(self.cur_device.model_ui))
  589.         elif self.cur_device.fax_type:
  590.             self.setWindowTitle(self._DevMgr5__tr('HP Device Manager - %1 (Printer)').arg(self.cur_device.model_ui))
  591.         else:
  592.             self.setWindowTitle(self._DevMgr5__tr('HP Device Manager - %1').arg(self.cur_device.model_ui))
  593.         self.statusBar().showMessage(self.cur_device_uri)
  594.  
  595.     
  596.     def updateDeviceByURI(self, device_uri):
  597.         return self.updateDevice(self.findDeviceByURI(device_uri))
  598.  
  599.     
  600.     def updateDevice(self, dev = None, update_tab = True):
  601.         ''' Update the device icon and currently displayed tab.
  602.         '''
  603.         if dev is None:
  604.             dev = self.cur_device
  605.         
  606.         log.debug('updateDevice(%s)' % dev.device_uri)
  607.         item = self.findItem(dev)
  608.         if item is not None:
  609.             item.setIcon(QIcon(self.createDeviceIcon(dev)))
  610.         
  611.         if dev is self.cur_device and update_tab:
  612.             self.updatePrinterCombos()
  613.             self.updateCurrentTab()
  614.             self.statusBar().showMessage(self.cur_device_uri)
  615.         
  616.  
  617.     
  618.     def DeviceList_currentChanged(self, i, j):
  619.         if i is not None and not (self.updating):
  620.             self.cur_device_uri = self.DeviceList.currentItem().device_uri
  621.             self.cur_device = device_list[self.cur_device_uri]
  622.             self.user_settings.last_used_device_uri = self.cur_device_uri
  623.             self.user_settings.save()
  624.             self.updateDevice()
  625.             self.updateWindowTitle()
  626.         
  627.  
  628.     
  629.     def findItem(self, dev):
  630.         if dev is None:
  631.             dev = self.cur_device
  632.         
  633.         return self.findItemByURI(dev.device_uri)
  634.  
  635.     
  636.     def findItemByURI(self, device_uri):
  637.         index = 0
  638.         item = self.DeviceList.item(index)
  639.         while item is not None:
  640.             if item.device_uri == device_uri:
  641.                 return item
  642.             index += 1
  643.             item = self.DeviceList.item(index)
  644.             continue
  645.             item.device_uri == device_uri
  646.  
  647.     
  648.     def findDeviceByURI(self, device_uri):
  649.         
  650.         try:
  651.             return device_list[device_uri]
  652.         except:
  653.             return None
  654.  
  655.  
  656.     
  657.     def requestDeviceUpdate(self, dev = None, item = None):
  658.         ''' Submit device update request to update thread. '''
  659.         if dev is None:
  660.             dev = self.cur_device
  661.         
  662.         if dev is not None:
  663.             dev.error_state = ERROR_STATE_REFRESHING
  664.             self.updateDevice(dev, update_tab = False)
  665.             self.sendMessage(dev.device_uri, '', EVENT_DEVICE_UPDATE_REQUESTED)
  666.         
  667.  
  668.     
  669.     def rescanDevices(self):
  670.         ''' Rescan and update all devices. '''
  671.         if not self.updating:
  672.             self.RefreshAllAction.setEnabled(False)
  673.             
  674.             try:
  675.                 self.refreshDeviceList()
  676.             finally:
  677.                 self.RefreshAllAction.setEnabled(True)
  678.  
  679.         
  680.  
  681.     
  682.     def callback(self):
  683.         qApp.processEvents()
  684.  
  685.     
  686.     def DeviceList_customContextMenuRequested(self, p):
  687.         d = self.cur_device
  688.         if d is not None:
  689.             if d.device_state != DEVICE_STATE_NOT_FOUND:
  690.                 pass
  691.             avail = d.supported
  692.             if d.device_type == DEVICE_TYPE_PRINTER:
  693.                 pass
  694.             printer = avail
  695.             if d.fax_type > FAX_TYPE_NONE and prop.fax_build and d.device_type == DEVICE_TYPE_FAX and sys.hexversion >= 33751280:
  696.                 pass
  697.             fax = avail
  698.             if d.scan_type > SCAN_TYPE_NONE and prop.scan_build and printer:
  699.                 pass
  700.             scan = self.user_settings.cmd_scan
  701.             if d.copy_type > COPY_TYPE_NONE:
  702.                 pass
  703.             cpy = printer
  704.             popup = QMenu(self)
  705.             item = self.DeviceList.currentItem()
  706.             if item is not None:
  707.                 if self.cur_device.error_state != ERROR_STATE_ERROR:
  708.                     if printer:
  709.                         popup.addAction((self._DevMgr5__tr('Print...'),), (lambda : self.contextMenuFunc(PrintDialog(self, self.cur_printer))))
  710.                         if scan:
  711.                             popup.addAction((self._DevMgr5__tr('Scan...'),), (lambda : self.contextMenuFunc(self.user_settings.cmd_scan)))
  712.                         
  713.                         if cpy:
  714.                             popup.addAction((self._DevMgr5__tr('Make Copies...'),), (lambda : MakeCopiesDialog(self, self.cur_device_uri)))
  715.                         
  716.                     elif fax:
  717.                         popup.addAction((self._DevMgr5__tr('Send Fax...'),), (lambda : self.contextMenuFunc(SendFaxDialog(self, self.cur_printer, self.cur_device_uri))))
  718.                     
  719.                     popup.addSeparator()
  720.                 
  721.                 if not self.updating:
  722.                     popup.addAction(self._DevMgr5__tr('Refresh Device'), self.requestDeviceUpdate)
  723.                 
  724.             
  725.             if not self.updating:
  726.                 popup.addAction(self._DevMgr5__tr('Refresh All'), self.rescanDevices)
  727.             
  728.             popup.addSeparator()
  729.             if self.DeviceList.viewMode() == QListView.IconMode:
  730.                 popup.addAction((self._DevMgr5__tr('View as List'),), (lambda : self.setDeviceListViewMode(QListView.ListMode)))
  731.             else:
  732.                 popup.addAction((self._DevMgr5__tr('View as Icons'),), (lambda : self.setDeviceListViewMode(QListView.IconMode)))
  733.             popup.exec_(self.DeviceList.mapToGlobal(p))
  734.         
  735.  
  736.     
  737.     def contextMenuFunc(self, f):
  738.         self.sendMessage('', '', EVENT_DEVICE_STOP_POLLING)
  739.         
  740.         try:
  741.             f.exec_()
  742.         except AttributeError:
  743.             beginWaitCursor()
  744.             if f.split(':')[0] in ('http', 'https', 'file'):
  745.                 log.debug('Opening browser to: %s' % item.cmd)
  746.                 utils.openURL(f)
  747.             else:
  748.                 self.runExternalCommand(f)
  749.             QTimer.singleShot(1000, self.unlockClick)
  750.         finally:
  751.             self.sendMessage('', '', EVENT_DEVICE_START_POLLING)
  752.  
  753.  
  754.     
  755.     def updatePrinterCombos(self):
  756.         self.PrintSettingsPrinterNameCombo.clear()
  757.         self.PrintControlPrinterNameCombo.clear()
  758.         if self.cur_device is not None and self.cur_device.supported:
  759.             self.cur_device.updateCUPSPrinters()
  760.             for c in self.cur_device.cups_printers:
  761.                 self.PrintSettingsPrinterNameCombo.insertItem(0, c.decode('utf-8'))
  762.                 self.PrintControlPrinterNameCombo.insertItem(0, c.decode('utf-8'))
  763.             
  764.             self.cur_printer = unicode(self.PrintSettingsPrinterNameCombo.currentText())
  765.         
  766.  
  767.     
  768.     def PrintSettingsPrinterNameCombo_activated(self, s):
  769.         self.cur_printer = unicode(s)
  770.         self.updateCurrentTab()
  771.  
  772.     
  773.     def PrintControlPrinterNameCombo_activated(self, s):
  774.         self.cur_printer = unicode(s)
  775.         self.updateCurrentTab()
  776.  
  777.     
  778.     def initActionsTab(self):
  779.         self.click_lock = None
  780.         self.ActionsList.setIconSize(QSize(32, 32))
  781.         self.connect(self.ActionsList, SIGNAL('itemClicked(QListWidgetItem *)'), self.ActionsList_clicked)
  782.         self.connect(self.ActionsList, SIGNAL('itemDoubleClicked(QListWidgetItem *)'), self.ActionsList_clicked)
  783.  
  784.     
  785.     def updateActionsTab(self):
  786.         beginWaitCursor()
  787.         
  788.         try:
  789.             self.ActionsList.clear()
  790.             d = self.cur_device
  791.             if d is not None:
  792.                 if d.device_state != DEVICE_STATE_NOT_FOUND:
  793.                     pass
  794.                 avail = d.supported
  795.                 if d.fax_type > FAX_TYPE_NONE and prop.fax_build and d.device_type == DEVICE_TYPE_FAX and sys.hexversion >= 33751280:
  796.                     pass
  797.                 fax = avail
  798.                 if d.device_type == DEVICE_TYPE_PRINTER:
  799.                     pass
  800.                 printer = avail
  801.                 if d.scan_type > SCAN_TYPE_NONE and prop.scan_build and printer:
  802.                     pass
  803.                 scan = self.user_settings.cmd_scan
  804.                 if d.copy_type > COPY_TYPE_NONE:
  805.                     pass
  806.                 cpy = printer
  807.                 req_plugin = d.plugin == PLUGIN_REQUIRED
  808.                 opt_plugin = d.plugin == PLUGIN_OPTIONAL
  809.                 
  810.                 try:
  811.                     (back_end, is_hp, bus, model, serial, dev_file, host, zc, port) = device.parseDeviceURI(self.cur_device_uri)
  812.                 except Error:
  813.                     return None
  814.  
  815.                 hplip_conf = ConfigParser.ConfigParser()
  816.                 fp = open('/etc/hp/hplip.conf', 'r')
  817.                 hplip_conf.readfp(fp)
  818.                 fp.close()
  819.                 
  820.                 try:
  821.                     plugin_installed = utils.to_bool(hplip_conf.get('hplip', 'plugin'))
  822.                 except ConfigParser.NoOptionError:
  823.                     plugin_installed = False
  824.  
  825.                 if d.plugin != PLUGIN_NONE:
  826.                     if req_plugin and plugin_installed:
  827.                         x = self._DevMgr5__tr('Download and install<br>required plugin (already installed).')
  828.                     elif req_plugin and not plugin_installed:
  829.                         x = self._DevMgr5__tr('Download and install<br>required plugin (needs installation).')
  830.                     elif opt_plugin and plugin_installed:
  831.                         x = self._DevMgr5__tr('Download and install<br>optional plugin (already installed).')
  832.                     elif opt_plugin and not plugin_installed:
  833.                         x = self._DevMgr5__tr('Download and install<br>optional plugin (needs installation).')
  834.                     
  835.                 else:
  836.                     x = ''
  837.                 self.ICONS = [
  838.                     None,
  839.                     None,
  840.                     None,
  841.                     None,
  842.                     None,
  843.                     None,
  844.                     None,
  845.                     None,
  846.                     None,
  847.                     None,
  848.                     None,
  849.                     None,
  850.                     None,
  851.                     None,
  852.                     None,
  853.                     None,
  854.                     None,
  855.                     None,
  856.                     ((None,), (((lambda : printer), self._DevMgr5__tr('Print'), 'print', (self._DevMgr5__tr('Print documents or files.'),), (lambda : PrintDialog(self, self.cur_printer))),), ((((lambda : scan), self._DevMgr5__tr('Scan'), 'scan', self._DevMgr5__tr('Scan a document, image, or photograph.<br>'), self.user_settings.cmd_scan),), ((((lambda : cpy), self._DevMgr5__tr('Make Copies'), 'makecopies', (self._DevMgr5__tr('Make copies on the device controlled by the PC.<br>'),), (lambda : MakeCopiesDialog(self, self.cur_device_uri))),), ((((((lambda : fax), self._DevMgr5__tr('Send Fax'), 'fax', (self._DevMgr5__tr('Send a fax from the PC.'),), (lambda : SendFaxDialog(self, self.cur_printer, self.cur_device_uri))),), ((lambda : fax), self._DevMgr5__tr('Fax Setup'), 'fax_setup', (self._DevMgr5__tr('Fax support must be setup before you can send faxes.'),), (lambda : FaxSetupDialog(self, self.cur_device_uri)))), ((lambda : if fax:
  857. passself.user_settings.cmd_fab), self._DevMgr5__tr('Fax Address Book'), 'fab', self._DevMgr5__tr('Setup fax phone numbers to use when sending faxes from the PC.'), self.user_settings.cmd_fab)), ((((((lambda : if d.power_settings != POWER_SETTINGS_NONE:
  858. passavail), self._DevMgr5__tr('Device Settings'), 'settings', (self._DevMgr5__tr('Your device has special device settings.<br>You may alter these settings here.'),), (lambda : DeviceSetupDialog(self, self.cur_device_uri))),), ((((((((lambda : printer), self._DevMgr5__tr('Print Test Page'), 'testpage', (self._DevMgr5__tr('Print a test page to test the setup of your printer.'),), (lambda : PrintTestPageDialog(self, self.cur_printer))), ((lambda : True), self._DevMgr5__tr('View Printer and Device Information'), 'cups', (self._DevMgr5__tr('View information about the device and all its CUPS queues.'),), (lambda : InfoDialog(self, self.cur_device_uri)))), ((lambda : if printer:
  859. passd.align_type != ALIGN_TYPE_NONE), self._DevMgr5__tr('Align Cartridges (Print Heads)'), 'align', (self._DevMgr5__tr('This will improve the quality of output when a new cartridge is installed.'),), (lambda : AlignDialog(self, self.cur_device_uri)))), ((lambda : if printer:
  860. passd.clean_type != CLEAN_TYPE_NONE), self._DevMgr5__tr('Clean Cartridges'), 'clean', (self._DevMgr5__tr('You only need to perform this action if you are<br>having problems with poor printout quality due to clogged ink nozzles.'),), (lambda : CleanDialog(self, self.cur_device_uri)))), ((lambda : if printer and d.color_cal_type != COLOR_CAL_TYPE_NONE:
  861. passd.color_cal_type == COLOR_CAL_TYPE_TYPHOON), self._DevMgr5__tr('Color Calibration'), 'colorcal', (self._DevMgr5__tr("Use this procedure to optimimize your printer's color output<br>(requires glossy photo paper)."),), (lambda : ColorCalDialog(self, self.cur_device_uri)))), ((lambda : if printer and d.color_cal_type != COLOR_CAL_TYPE_NONE:
  862. passd.color_cal_type != COLOR_CAL_TYPE_TYPHOON), self._DevMgr5__tr('Color Calibration'), 'colorcal', (self._DevMgr5__tr("Use this procedure to optimimize your printer's color output."),), (lambda : ColorCalDialog(self, self.cur_device_uri)))), ((lambda : if printer:
  863. passd.linefeed_cal_type != LINEFEED_CAL_TYPE_NONE), self._DevMgr5__tr('Line Feed Calibration'), 'linefeed_cal', (self._DevMgr5__tr('Use line feed calibration to optimize print quality<br>(to remove gaps in the printed output).'),), (lambda : LineFeedCalDialog(self, self.cur_device_uri)))), ((lambda : if printer:
  864. passd.pq_diag_type != PQ_DIAG_TYPE_NONE), self._DevMgr5__tr('Print Diagnostic Page'), 'pq_diag', (self._DevMgr5__tr('Your printer can print a test page <br>to help diagnose print quality problems.'),), (lambda : PQDiagDialog(self, self.cur_device_uri)))), ((lambda : if printer and d.wifi_config >= WIFI_CONFIG_USB_XML:
  865. passbus == 'usb'), self._DevMgr5__tr('Wireless/wifi setup using USB'), 'wireless', self._DevMgr5__tr('Configure your wireless capable printer using a temporary USB connection.'), 'hp-wificonfig -d %s' % self.cur_device_uri)), ((lambda : if printer:
  866. passd.fw_download), self._DevMgr5__tr('Download Firmware'), 'firmware', (self._DevMgr5__tr('Download firmware to your printer <br>(required on some devices after each power-up).'),), (lambda : FirmwareDialog(self, self.cur_device_uri)))), (lambda : if printer:
  867. passreq_plugin), (self._DevMgr5__tr('Install Required Plugin'), 'plugin', x), (lambda : PluginInstall(self, d.plugin, plugin_installed)))), (lambda : if printer:
  868. passopt_plugin), (self._DevMgr5__tr('Install Optional Plugin'), 'plugin', x), (lambda : PluginInstall(self, d.plugin, plugin_installed)))),
  869.                     ((lambda : if printer and d.embedded_server_type > EWS_NONE:
  870. passbus == 'net'), self._DevMgr5__tr("Open printer's web page in a browser"), 'ews', self._DevMgr5__tr("The printer's web page has supply, status, and other information."), openEWS(host, zc)),
  871.                     ((lambda : True), self._DevMgr5__tr('Visit HPLIP Support Website'), 'hp_logo', self._DevMgr5__tr('Visit HPLIP Support Website.'), self.support),
  872.                     ((lambda : True), self._DevMgr5__tr('Help'), 'help', self._DevMgr5__tr('View HPLIP help.'), self.docs)]
  873.                 if not self.func_icons_cached:
  874.                     for filter, text, icon, tooltip, cmd in self.ICONS:
  875.                         self.func_icons[icon] = load_pixmap(icon, '32x32')
  876.                     
  877.                     self.func_icons_cached = True
  878.                 
  879.                 for filter, text, icon, tooltip, cmd in self.ICONS:
  880.                     if filter is not None:
  881.                         if not filter():
  882.                             continue
  883.                         
  884.                     
  885.                     FuncViewItem(self.ActionsList, text, self.func_icons[icon], tooltip, cmd)
  886.                 
  887.         finally:
  888.             endWaitCursor()
  889.  
  890.  
  891.     
  892.     def ActionsList_clicked(self, item):
  893.         if item is not None and self.click_lock is not item:
  894.             self.click_lock = item
  895.             if item.cmd and callable(item.cmd):
  896.                 dlg = item.cmd()
  897.                 self.sendMessage('', '', EVENT_DEVICE_STOP_POLLING)
  898.                 
  899.                 try:
  900.                     dlg.exec_()
  901.                 finally:
  902.                     self.sendMessage('', '', EVENT_DEVICE_START_POLLING)
  903.  
  904.             else:
  905.                 beginWaitCursor()
  906.                 if item.cmd.split(':')[0] in ('http', 'https', 'file'):
  907.                     log.debug('Opening browser to: %s' % item.cmd)
  908.                     utils.openURL(item.cmd)
  909.                 else:
  910.                     self.runExternalCommand(item.cmd)
  911.             QTimer.singleShot(1000, self.unlockClick)
  912.         
  913.  
  914.     
  915.     def unlockClick(self):
  916.         self.click_lock = None
  917.         endWaitCursor()
  918.  
  919.     
  920.     def ActionsList_customContextMenuRequested(self, p):
  921.         print p
  922.  
  923.     
  924.     def initStatusTab(self):
  925.         self.StatusTable.setColumnCount(0)
  926.         self.status_headers = [
  927.             self._DevMgr5__tr(''),
  928.             self._DevMgr5__tr('Status'),
  929.             self._DevMgr5__tr('Date and Time'),
  930.             self._DevMgr5__tr('Code'),
  931.             self._DevMgr5__tr('Job ID'),
  932.             self._DevMgr5__tr('Description')]
  933.  
  934.     
  935.     def updateStatusTab(self):
  936.         self.updateStatusLCD()
  937.         self.updateStatusTable()
  938.  
  939.     
  940.     def updateStatusLCD(self):
  941.         if self.cur_device is not None and self.cur_device.hist and self.cur_device.supported:
  942.             dq = self.cur_device.dq
  943.             if dq.get('panel', 0) == 1:
  944.                 line1 = dq.get('panel-line1', '')
  945.                 line2 = dq.get('panel-line2', '')
  946.             else:
  947.                 
  948.                 try:
  949.                     line1 = device.queryString(self.cur_device.hist[0].event_code)
  950.                 except (AttributeError, TypeError):
  951.                     line1 = ''
  952.  
  953.                 line2 = ''
  954.             self.drawStatusLCD(line1, line2)
  955.         elif self.cur_device.status_type == STATUS_TYPE_NONE:
  956.             self.drawStatusLCD(self._DevMgr5__tr('Status information not'), self._DevMgr5__tr('available for this device.'))
  957.         elif not self.cur_device.supported:
  958.             self.drawStatusLCD(self._DevMgr5__tr('Device not supported.'))
  959.         elif not self.cur_device.hist:
  960.             self.drawStatusLCD(self._DevMgr5__tr('No status history available.'))
  961.         else:
  962.             self.drawStatusLCD()
  963.  
  964.     
  965.     def drawStatusLCD(self, line1 = '', line2 = ''):
  966.         pm = load_pixmap('panel_lcd', 'other')
  967.         p = QPainter()
  968.         p.begin(pm)
  969.         p.setPen(QColor(0, 0, 0))
  970.         p.setFont(self.font())
  971.         (x, y_line1, y_line2) = (10, 17, 33)
  972.         if line1:
  973.             p.drawText(x, y_line1, line1)
  974.         
  975.         if line2:
  976.             p.drawText(x, y_line2, line2)
  977.         
  978.         p.end()
  979.         self.LCD.setPixmap(pm)
  980.  
  981.     
  982.     def updateStatusTable(self):
  983.         self.StatusTable.clear()
  984.         flags = Qt.ItemIsSelectable | Qt.ItemIsEnabled
  985.         row = 0
  986.         hist = self.cur_device.hist[:]
  987.         if hist:
  988.             self.StatusTable.setRowCount(len(hist))
  989.             self.StatusTable.setColumnCount(len(self.status_headers))
  990.             self.StatusTable.setHorizontalHeaderLabels(self.status_headers)
  991.             self.StatusTable.verticalHeader().hide()
  992.             self.StatusTable.horizontalHeader().show()
  993.             hist.reverse()
  994.             row = len(hist) - 1
  995.             for e in hist:
  996.                 if e is None:
  997.                     continue
  998.                 
  999.                 ess = device.queryString(e.event_code, 0)
  1000.                 esl = device.queryString(e.event_code, 1)
  1001.                 if row == 0:
  1002.                     desc = self._DevMgr5__tr('(most recent)')
  1003.                 else:
  1004.                     desc = getTimeDeltaDesc(e.timedate)
  1005.                 dt = QDateTime()
  1006.                 dt.setTime_t(int(e.timedate))
  1007.                 tt = QString('%1 %2').arg(dt.toString()).arg(desc)
  1008.                 if e.job_id:
  1009.                     job_id = unicode(e.job_id)
  1010.                 else:
  1011.                     job_id = u''
  1012.                 error_state = STATUS_TO_ERROR_STATE_MAP.get(e.event_code, ERROR_STATE_CLEAR)
  1013.                 tech_type = self.cur_device.tech_type
  1014.                 if tech_type in (TECH_TYPE_COLOR_INK, TECH_TYPE_MONO_INK):
  1015.                     status_pix = getStatusListIcon(error_state)[0]
  1016.                 else:
  1017.                     status_pix = getStatusListIcon(error_state)[1]
  1018.                 event_code = unicode(e.event_code)
  1019.                 i = QTableWidgetItem(QIcon(status_pix), self._DevMgr5__tr(''))
  1020.                 i.setFlags(flags)
  1021.                 self.StatusTable.setItem(row, 0, i)
  1022.                 for col, t in [
  1023.                     (1, ess),
  1024.                     (2, tt),
  1025.                     (3, event_code),
  1026.                     (4, job_id),
  1027.                     (5, esl)]:
  1028.                     i = QTableWidgetItem(QString(t))
  1029.                     i.setFlags(flags)
  1030.                     self.StatusTable.setItem(row, col, i)
  1031.                 
  1032.                 row -= 1
  1033.             
  1034.             self.StatusTable.resizeColumnsToContents()
  1035.             self.StatusTable.setColumnWidth(0, 24)
  1036.         else:
  1037.             self.StatusTable.setRowCount(1)
  1038.             self.StatusTable.setColumnCount(2)
  1039.             self.StatusTable.setHorizontalHeaderLabels([
  1040.                 '',
  1041.                 ''])
  1042.             self.StatusTable.verticalHeader().hide()
  1043.             self.StatusTable.horizontalHeader().hide()
  1044.             flags = Qt.ItemIsEnabled
  1045.             pixmap = getStatusListIcon(ERROR_STATE_ERROR)[0]
  1046.             i = QTableWidgetItem(QIcon(pixmap), self._DevMgr5__tr(''))
  1047.             i.setFlags(flags)
  1048.             self.StatusTable.setItem(row, 0, i)
  1049.             i = QTableWidgetItem(self._DevMgr5__tr('Status information not available for this device.'))
  1050.             i.setFlags(flags)
  1051.             self.StatusTable.setItem(0, 1, i)
  1052.             self.StatusTable.resizeColumnsToContents()
  1053.             self.StatusTable.setColumnWidth(0, 24)
  1054.  
  1055.     
  1056.     def initSuppliesTab(self):
  1057.         self.pix_battery = load_pixmap('battery', '16x16')
  1058.         yellow = '#ffff00'
  1059.         light_yellow = '#ffffcc'
  1060.         cyan = '#00ffff'
  1061.         light_cyan = '#ccffff'
  1062.         magenta = '#ff00ff'
  1063.         light_magenta = '#ffccff'
  1064.         black = '#000000'
  1065.         blue = '#0000ff'
  1066.         dark_grey = '#808080'
  1067.         light_grey = '#c0c0c0'
  1068.         self.TYPE_TO_PIX_MAP = {
  1069.             AGENT_TYPE_UNSPECIFIED: [
  1070.                 black],
  1071.             AGENT_TYPE_BLACK: [
  1072.                 black],
  1073.             AGENT_TYPE_CMY: [
  1074.                 cyan,
  1075.                 magenta,
  1076.                 yellow],
  1077.             AGENT_TYPE_KCM: [
  1078.                 light_cyan,
  1079.                 light_magenta,
  1080.                 light_yellow],
  1081.             AGENT_TYPE_GGK: [
  1082.                 dark_grey],
  1083.             AGENT_TYPE_YELLOW: [
  1084.                 yellow],
  1085.             AGENT_TYPE_MAGENTA: [
  1086.                 magenta],
  1087.             AGENT_TYPE_CYAN: [
  1088.                 cyan],
  1089.             AGENT_TYPE_CYAN_LOW: [
  1090.                 light_cyan],
  1091.             AGENT_TYPE_YELLOW_LOW: [
  1092.                 light_yellow],
  1093.             AGENT_TYPE_MAGENTA_LOW: [
  1094.                 light_magenta],
  1095.             AGENT_TYPE_BLUE: [
  1096.                 blue],
  1097.             AGENT_TYPE_KCMY_CM: [
  1098.                 yellow,
  1099.                 cyan,
  1100.                 magenta],
  1101.             AGENT_TYPE_LC_LM: [
  1102.                 light_cyan,
  1103.                 light_magenta],
  1104.             AGENT_TYPE_LG_PK: [
  1105.                 light_grey,
  1106.                 dark_grey],
  1107.             AGENT_TYPE_LG: [
  1108.                 light_grey],
  1109.             AGENT_TYPE_G: [
  1110.                 dark_grey],
  1111.             AGENT_TYPE_PG: [
  1112.                 light_grey],
  1113.             AGENT_TYPE_C_M: [
  1114.                 cyan,
  1115.                 magenta],
  1116.             AGENT_TYPE_K_Y: [
  1117.                 black,
  1118.                 yellow] }
  1119.         self.supplies_headers = [
  1120.             self._DevMgr5__tr(''),
  1121.             self._DevMgr5__tr('Description'),
  1122.             self._DevMgr5__tr('HP Part No.'),
  1123.             self._DevMgr5__tr('Approx. Level'),
  1124.             self._DevMgr5__tr('Status')]
  1125.  
  1126.     
  1127.     def updateSuppliesTab(self):
  1128.         beginWaitCursor()
  1129.         flags = Qt.ItemIsSelectable | Qt.ItemIsEnabled
  1130.         
  1131.         try:
  1132.             self.SuppliesTable.clear()
  1133.             self.SuppliesTable.setRowCount(0)
  1134.             self.SuppliesTable.setColumnCount(0)
  1135.             if self.cur_device is not None and self.cur_device.supported and self.cur_device.status_type != STATUS_TYPE_NONE and self.cur_device.device_state != DEVICE_STATE_NOT_FOUND:
  1136.                 
  1137.                 try:
  1138.                     self.cur_device.sorted_supplies
  1139.                 except AttributeError:
  1140.                     self.cur_device.sorted_supplies = []
  1141.  
  1142.                 if not self.cur_device.sorted_supplies:
  1143.                     a = 1
  1144.                     while True:
  1145.                         
  1146.                         try:
  1147.                             agent_type = int(self.cur_device.dq['agent%d-type' % a])
  1148.                             agent_kind = int(self.cur_device.dq['agent%d-kind' % a])
  1149.                             agent_sku = self.cur_device.dq['agent%d-sku' % a]
  1150.                         except KeyError:
  1151.                             break
  1152.  
  1153.                         self.cur_device.sorted_supplies.append((a, agent_kind, agent_type, agent_sku))
  1154.                         a += 1
  1155.                     self.cur_device.sorted_supplies.sort((lambda x, y: if not cmp(x[1], y[1]):
  1156. passcmp(x[3], y[3])))
  1157.                 
  1158.                 self.SuppliesTable.setRowCount(len(self.cur_device.sorted_supplies))
  1159.                 self.SuppliesTable.setColumnCount(len(self.supplies_headers))
  1160.                 self.SuppliesTable.setHorizontalHeaderLabels(self.supplies_headers)
  1161.                 self.SuppliesTable.verticalHeader().hide()
  1162.                 self.SuppliesTable.horizontalHeader().show()
  1163.                 self.SuppliesTable.setIconSize(QSize(100, 18))
  1164.                 for row, x in enumerate(self.cur_device.sorted_supplies):
  1165.                     (a, agent_kind, agent_type, agent_sku) = x
  1166.                     agent_level = int(self.cur_device.dq['agent%d-level' % a])
  1167.                     agent_desc = self.cur_device.dq['agent%d-desc' % a]
  1168.                     agent_health_desc = self.cur_device.dq['agent%d-health-desc' % a]
  1169.                     level_pixmap = None
  1170.                     if agent_kind in (AGENT_KIND_SUPPLY, AGENT_KIND_HEAD_AND_SUPPLY, AGENT_KIND_TONER_CARTRIDGE, AGENT_KIND_MAINT_KIT, AGENT_KIND_ADF_KIT, AGENT_KIND_INT_BATTERY, AGENT_KIND_DRUM_KIT):
  1171.                         level_pixmap = self.createStatusLevelGraphic(agent_level, agent_type)
  1172.                     
  1173.                     pixmap = None
  1174.                     if agent_kind in (AGENT_KIND_SUPPLY, AGENT_KIND_HEAD, AGENT_KIND_HEAD_AND_SUPPLY, AGENT_KIND_TONER_CARTRIDGE, AGENT_KIND_INT_BATTERY):
  1175.                         pixmap = self.getStatusIcon(agent_kind, agent_type)
  1176.                     
  1177.                     if pixmap is not None:
  1178.                         i = QTableWidgetItem(QIcon(pixmap), self._DevMgr5__tr(''))
  1179.                         i.setFlags(flags)
  1180.                         self.SuppliesTable.setItem(row, 0, i)
  1181.                     
  1182.                     for col, t in [
  1183.                         (1, agent_desc),
  1184.                         (2, agent_sku),
  1185.                         (4, agent_health_desc)]:
  1186.                         i = QTableWidgetItem(QString(t))
  1187.                         i.setFlags(flags)
  1188.                         self.SuppliesTable.setItem(row, col, i)
  1189.                     
  1190.                     if level_pixmap is not None:
  1191.                         i = QTableWidgetItem(QIcon(level_pixmap), self._DevMgr5__tr(''))
  1192.                         i.setFlags(flags)
  1193.                         self.SuppliesTable.setItem(row, 3, i)
  1194.                         continue
  1195.                 
  1196.                 self.SuppliesTable.resizeColumnsToContents()
  1197.                 self.SuppliesTable.setColumnWidth(0, 24)
  1198.                 self.SuppliesTable.setColumnWidth(3, 120)
  1199.             else:
  1200.                 log.warning('Supplies information not available for this device.')
  1201.                 flags = Qt.ItemIsEnabled
  1202.                 self.SuppliesTable.setRowCount(1)
  1203.                 self.SuppliesTable.setColumnCount(2)
  1204.                 self.SuppliesTable.setHorizontalHeaderLabels([
  1205.                     '',
  1206.                     ''])
  1207.                 self.SuppliesTable.verticalHeader().hide()
  1208.                 self.SuppliesTable.horizontalHeader().hide()
  1209.                 i = QTableWidgetItem(self._DevMgr5__tr('Supplies information not available for this device.'))
  1210.                 i.setFlags(flags)
  1211.                 self.SuppliesTable.setItem(0, 1, i)
  1212.                 pixmap = getStatusListIcon(ERROR_STATE_ERROR)[0]
  1213.                 i = QTableWidgetItem(QIcon(pixmap), self._DevMgr5__tr(''))
  1214.                 i.setFlags(flags)
  1215.                 self.SuppliesTable.setItem(0, 0, i)
  1216.                 self.SuppliesTable.resizeColumnsToContents()
  1217.                 self.SuppliesTable.setColumnWidth(0, 24)
  1218.         finally:
  1219.             endWaitCursor()
  1220.  
  1221.  
  1222.     
  1223.     def getStatusIcon(self, agent_kind, agent_type):
  1224.         if agent_kind in (AGENT_KIND_SUPPLY, AGENT_KIND_HEAD, AGENT_KIND_HEAD_AND_SUPPLY, AGENT_KIND_TONER_CARTRIDGE):
  1225.             map = self.TYPE_TO_PIX_MAP[agent_type]
  1226.             if isinstance(map, list):
  1227.                 map_len = len(map)
  1228.                 pix = QPixmap(16, 16)
  1229.                 pix.fill(QColor(0, 0, 0, 0))
  1230.                 p = QPainter()
  1231.                 p.begin(pix)
  1232.                 p.setRenderHint(QPainter.Antialiasing)
  1233.                 if map_len == 1:
  1234.                     p.setPen(QColor(map[0]))
  1235.                     p.setBrush(QBrush(QColor(map[0]), Qt.SolidPattern))
  1236.                     p.drawPie(2, 2, 10, 10, 0, 5760)
  1237.                 elif map_len == 2:
  1238.                     p.setPen(QColor(map[0]))
  1239.                     p.setBrush(QBrush(QColor(map[0]), Qt.SolidPattern))
  1240.                     p.drawPie(2, 4, 8, 8, 0, 5760)
  1241.                     p.setPen(QColor(map[1]))
  1242.                     p.setBrush(QBrush(QColor(map[1]), Qt.SolidPattern))
  1243.                     p.drawPie(6, 4, 8, 8, 0, 5760)
  1244.                 elif map_len == 3:
  1245.                     p.setPen(QColor(map[2]))
  1246.                     p.setBrush(QBrush(QColor(map[2]), Qt.SolidPattern))
  1247.                     p.drawPie(6, 6, 8, 8, 0, 5760)
  1248.                     p.setPen(QColor(map[1]))
  1249.                     p.setBrush(QBrush(QColor(map[1]), Qt.SolidPattern))
  1250.                     p.drawPie(2, 6, 8, 8, 0, 5760)
  1251.                     p.setPen(QColor(map[0]))
  1252.                     p.setBrush(QBrush(QColor(map[0]), Qt.SolidPattern))
  1253.                     p.drawPie(4, 2, 8, 8, 0, 5760)
  1254.                 
  1255.                 p.end()
  1256.                 return pix
  1257.             return map
  1258.         agent_kind in (AGENT_KIND_SUPPLY, AGENT_KIND_HEAD, AGENT_KIND_HEAD_AND_SUPPLY, AGENT_KIND_TONER_CARTRIDGE)
  1259.         if agent_kind == AGENT_KIND_INT_BATTERY:
  1260.             return self.pix_battery
  1261.  
  1262.     
  1263.     def createStatusLevelGraphic(self, percent, agent_type, w = 100, h = 18):
  1264.         if percent:
  1265.             fw = (w / 100) * percent
  1266.         else:
  1267.             fw = 0
  1268.         px = QPixmap(w, h)
  1269.         px.fill(QColor(0, 0, 0, 0))
  1270.         pp = QPainter()
  1271.         pp.begin(px)
  1272.         pp.setRenderHint(QPainter.Antialiasing)
  1273.         pp.setPen(Qt.black)
  1274.         map = self.TYPE_TO_PIX_MAP[agent_type]
  1275.         map_len = len(map)
  1276.         if map_len == 1 or map_len > 3:
  1277.             pp.fillRect(0, 0, fw, h, QBrush(QColor(map[0])))
  1278.         elif map_len == 2:
  1279.             h2 = h / 2
  1280.             pp.fillRect(0, 0, fw, h2, QBrush(QColor(map[0])))
  1281.             pp.fillRect(0, h2, fw, h, QBrush(QColor(map[1])))
  1282.         elif map_len == 3:
  1283.             h3 = h / 3
  1284.             h23 = 2 * h3
  1285.             pp.fillRect(0, 0, fw, h3, QBrush(QColor(map[0])))
  1286.             pp.fillRect(0, h3, fw, h23, QBrush(QColor(map[1])))
  1287.             pp.fillRect(0, h23, fw, h, QBrush(QColor(map[2])))
  1288.         
  1289.         pp.drawRect(0, 0, w, h)
  1290.         if percent > 75 and agent_type in (AGENT_TYPE_BLACK, AGENT_TYPE_UNSPECIFIED, AGENT_TYPE_BLUE):
  1291.             pp.setPen(Qt.white)
  1292.         
  1293.         w1 = 3 * w / 4
  1294.         h6 = h / 6
  1295.         pp.drawLine(w1, 0, w1, h6)
  1296.         pp.drawLine(w1, h, w1, h - h6)
  1297.         if percent > 50 and agent_type in (AGENT_TYPE_BLACK, AGENT_TYPE_UNSPECIFIED, AGENT_TYPE_BLUE):
  1298.             pp.setPen(Qt.white)
  1299.         
  1300.         w2 = w / 2
  1301.         h4 = h / 4
  1302.         pp.drawLine(w2, 0, w2, h4)
  1303.         pp.drawLine(w2, h, w2, h - h4)
  1304.         if percent > 25 and agent_type in (AGENT_TYPE_BLACK, AGENT_TYPE_UNSPECIFIED, AGENT_TYPE_BLUE):
  1305.             pp.setPen(Qt.white)
  1306.         
  1307.         w4 = w / 4
  1308.         pp.drawLine(w4, 0, w4, h6)
  1309.         pp.drawLine(w4, h, w4, h - h6)
  1310.         pp.end()
  1311.         return px
  1312.  
  1313.     
  1314.     def initPrintSettingsTab(self):
  1315.         pass
  1316.  
  1317.     
  1318.     def updatePrintSettingsTab(self):
  1319.         beginWaitCursor()
  1320.         
  1321.         try:
  1322.             if self.cur_device.device_type == DEVICE_TYPE_PRINTER:
  1323.                 self.PrintSettingsPrinterNameLabel.setText(self._DevMgr5__tr('Printer Name:'))
  1324.             else:
  1325.                 self.PrintSettingsPrinterNameLabel.setText(self._DevMgr5__tr('Fax Name:'))
  1326.             self.PrintSettingsToolbox.updateUi(self.cur_device, self.cur_printer)
  1327.         finally:
  1328.             endWaitCursor()
  1329.  
  1330.  
  1331.     
  1332.     def initPrintControlTab(self):
  1333.         self.JOB_STATES = {
  1334.             cups.IPP_JOB_PENDING: self._DevMgr5__tr('Pending'),
  1335.             cups.IPP_JOB_HELD: self._DevMgr5__tr('On hold'),
  1336.             cups.IPP_JOB_PROCESSING: self._DevMgr5__tr('Printing'),
  1337.             cups.IPP_JOB_STOPPED: self._DevMgr5__tr('Stopped'),
  1338.             cups.IPP_JOB_CANCELLED: self._DevMgr5__tr('Canceled'),
  1339.             cups.IPP_JOB_ABORTED: self._DevMgr5__tr('Aborted'),
  1340.             cups.IPP_JOB_COMPLETED: self._DevMgr5__tr('Completed') }
  1341.         self.CancelJobButton.setIcon(QIcon(load_pixmap('cancel', '16x16')))
  1342.         self.RefreshButton.setIcon(QIcon(load_pixmap('refresh', '16x16')))
  1343.         self.JOB_STATE_ICONS = {
  1344.             cups.IPP_JOB_PENDING: QIcon(load_pixmap('busy', '16x16')),
  1345.             cups.IPP_JOB_HELD: QIcon(load_pixmap('busy', '16x16')),
  1346.             cups.IPP_JOB_PROCESSING: QIcon(load_pixmap('print', '16x16')),
  1347.             cups.IPP_JOB_STOPPED: QIcon(load_pixmap('warning', '16x16')),
  1348.             cups.IPP_JOB_CANCELLED: QIcon(load_pixmap('warning', '16x16')),
  1349.             cups.IPP_JOB_ABORTED: QIcon(load_pixmap('error', '16x16')),
  1350.             cups.IPP_JOB_COMPLETED: QIcon(load_pixmap('ok', '16x16')) }
  1351.         self.connect(self.StartStopButton, SIGNAL('clicked()'), self.StartStopButton_clicked)
  1352.         self.connect(self.AcceptRejectButton, SIGNAL('clicked()'), self.AcceptRejectButton_clicked)
  1353.         self.connect(self.SetDefaultButton, SIGNAL('clicked()'), self.SetDefaultButton_clicked)
  1354.         self.connect(self.CancelJobButton, SIGNAL('clicked()'), self.CancelJobButton_clicked)
  1355.         self.connect(self.RefreshButton, SIGNAL('clicked()'), self.RefreshButton_clicked)
  1356.         self.job_headers = [
  1357.             self._DevMgr5__tr('Status'),
  1358.             self._DevMgr5__tr('Title/Description'),
  1359.             self._DevMgr5__tr('Job ID')]
  1360.  
  1361.     
  1362.     def CancelJobButton_clicked(self):
  1363.         item = self.JobTable.currentItem()
  1364.         if item is not None:
  1365.             (job_id, ok) = item.data(Qt.UserRole).toInt()
  1366.             if ok and job_id:
  1367.                 self.cur_device.cancelJob(job_id)
  1368.                 QTimer.singleShot(1000, self.updatePrintControlTab)
  1369.             
  1370.         
  1371.  
  1372.     
  1373.     def RefreshButton_clicked(self):
  1374.         self.updatePrintControlTab()
  1375.  
  1376.     
  1377.     def updatePrintControlTab(self):
  1378.         if self.cur_device.device_type == DEVICE_TYPE_PRINTER:
  1379.             self.PrintControlPrinterNameLabel.setText(self._DevMgr5__tr('Printer Name:'))
  1380.         else:
  1381.             self.PrintControlPrinterNameLabel.setText(self._DevMgr5__tr('Fax Name:'))
  1382.         self.JobTable.clear()
  1383.         self.JobTable.setRowCount(0)
  1384.         self.JobTable.setColumnCount(0)
  1385.         self.updatePrintController()
  1386.         flags = Qt.ItemIsSelectable | Qt.ItemIsEnabled
  1387.         jobs = cups.getJobs()
  1388.         num_jobs = 0
  1389.         for j in jobs:
  1390.             if j.dest.decode('utf-8') == unicode(self.cur_printer):
  1391.                 num_jobs += 1
  1392.                 continue
  1393.         
  1394.         if num_jobs:
  1395.             self.CancelJobButton.setEnabled(True)
  1396.             self.JobTable.setRowCount(num_jobs)
  1397.             self.JobTable.setColumnCount(len(self.job_headers))
  1398.             self.JobTable.setHorizontalHeaderLabels(self.job_headers)
  1399.             for row, j in enumerate(jobs):
  1400.                 if j.dest.decode('utf-8') == unicode(self.cur_printer):
  1401.                     i = QTableWidgetItem(self.JOB_STATE_ICONS[j.state], self.JOB_STATES[j.state])
  1402.                     i.setData(Qt.UserRole, QVariant(j.id))
  1403.                     i.setFlags(flags)
  1404.                     self.JobTable.setItem(row, 0, i)
  1405.                     i = QTableWidgetItem(j.title)
  1406.                     i.setFlags(flags)
  1407.                     self.JobTable.setItem(row, 1, i)
  1408.                     i = QTableWidgetItem(unicode(j.id))
  1409.                     i.setFlags(flags)
  1410.                     self.JobTable.setItem(row, 2, i)
  1411.                     continue
  1412.             
  1413.             self.JobTable.setCurrentCell(0, 0)
  1414.             self.JobTable.resizeColumnsToContents()
  1415.         else:
  1416.             self.CancelJobButton.setEnabled(False)
  1417.  
  1418.     
  1419.     def getPrinterState(self):
  1420.         self.printer_state = cups.IPP_PRINTER_STATE_IDLE
  1421.         self.printer_accepting = True
  1422.         cups_printers = cups.getPrinters()
  1423.         for p in cups_printers:
  1424.             if p.name.decode('utf-8') == self.cur_printer:
  1425.                 self.printer_state = p.state
  1426.                 self.printer_accepting = p.accepting
  1427.                 break
  1428.                 continue
  1429.         
  1430.  
  1431.     
  1432.     def updatePrintController(self):
  1433.         self.SetDefaultButton.setText(self._DevMgr5__tr('Set as Default'))
  1434.         default_printer = cups.getDefaultPrinter()
  1435.         if default_printer is not None:
  1436.             default_printer = default_printer.decode('utf8')
  1437.         
  1438.         if default_printer == self.cur_printer:
  1439.             self.SetDefaultLabel.setText(self._DevMgr5__tr('Default Printer'))
  1440.             self.SetDefaultIcon.setPixmap(load_pixmap('ok', '16x16'))
  1441.             self.SetDefaultButton.setEnabled(False)
  1442.         else:
  1443.             self.SetDefaultLabel.setText(self._DevMgr5__tr('Not Default Printer'))
  1444.             self.SetDefaultIcon.setPixmap(load_pixmap('info', '16x16'))
  1445.             self.SetDefaultButton.setEnabled(True)
  1446.         self.getPrinterState()
  1447.         if self.printer_state == cups.IPP_PRINTER_STATE_IDLE:
  1448.             self.StartStopLabel.setText(self._DevMgr5__tr('Started/Idle'))
  1449.             self.StartStopIcon.setPixmap(load_pixmap('idle', '16x16'))
  1450.             if self.cur_device.device_type == DEVICE_TYPE_PRINTER:
  1451.                 self.StartStopButton.setText(self._DevMgr5__tr('Stop Printer'))
  1452.             else:
  1453.                 self.StartStopButton.setText(self._DevMgr5__tr('Stop Fax'))
  1454.         elif self.printer_state == cups.IPP_PRINTER_STATE_PROCESSING:
  1455.             self.StartStopLabel.setText(self._DevMgr5__tr('Started/Processing'))
  1456.             self.StartStopIcon.setPixmap(load_pixmap('busy', '16x16'))
  1457.             if self.cur_device.device_type == DEVICE_TYPE_PRINTER:
  1458.                 self.StartStopButton.setText(self._DevMgr5__tr('Stop Printer'))
  1459.             else:
  1460.                 self.StartStopButton.setText(self._DevMgr5__tr('Stop Fax'))
  1461.         else:
  1462.             self.StartStopLabel.setText(self._DevMgr5__tr('Stopped'))
  1463.             self.StartStopIcon.setPixmap(load_pixmap('warning', '16x16'))
  1464.             if self.cur_device.device_type == DEVICE_TYPE_PRINTER:
  1465.                 self.StartStopButton.setText(self._DevMgr5__tr('Start Printer'))
  1466.             else:
  1467.                 self.StartStopButton.setText(self._DevMgr5__tr('Start Fax'))
  1468.         if self.printer_accepting:
  1469.             self.AcceptRejectLabel.setText(self._DevMgr5__tr('Accepting Jobs'))
  1470.             self.AcceptRejectIcon.setPixmap(load_pixmap('idle', '16x16'))
  1471.             self.AcceptRejectButton.setText(self._DevMgr5__tr('Reject Jobs'))
  1472.         else:
  1473.             self.AcceptRejectLabel.setText(self._DevMgr5__tr('Rejecting Jobs'))
  1474.             self.AcceptRejectIcon.setPixmap(load_pixmap('warning', '16x16'))
  1475.             self.AcceptRejectButton.setText(self._DevMgr5__tr('Accept Jobs'))
  1476.  
  1477.     
  1478.     def StartStopButton_clicked(self):
  1479.         beginWaitCursor()
  1480.         
  1481.         try:
  1482.             if self.printer_state in (cups.IPP_PRINTER_STATE_IDLE, cups.IPP_PRINTER_STATE_PROCESSING):
  1483.                 result = cups.stop(self.cur_printer)
  1484.                 if result:
  1485.                     if self.cur_device.device_type == DEVICE_TYPE_PRINTER:
  1486.                         e = EVENT_PRINTER_QUEUE_STOPPED
  1487.                     else:
  1488.                         e = EVENT_FAX_QUEUE_STOPPED
  1489.                 
  1490.             else:
  1491.                 result = cups.start(self.cur_printer)
  1492.                 if result:
  1493.                     if self.cur_device.device_type == DEVICE_TYPE_PRINTER:
  1494.                         e = EVENT_PRINTER_QUEUE_STARTED
  1495.                     else:
  1496.                         e = EVENT_FAX_QUEUE_STARTED
  1497.                 
  1498.             if result:
  1499.                 self.updatePrintController()
  1500.                 self.cur_device.sendEvent(e, self.cur_printer)
  1501.             else:
  1502.                 log.error('Start/Stop printer operation failed')
  1503.         finally:
  1504.             endWaitCursor()
  1505.  
  1506.  
  1507.     
  1508.     def AcceptRejectButton_clicked(self):
  1509.         beginWaitCursor()
  1510.         
  1511.         try:
  1512.             if self.printer_accepting:
  1513.                 result = cups.reject(self.cur_printer)
  1514.                 if result:
  1515.                     if self.cur_device.device_type == DEVICE_TYPE_PRINTER:
  1516.                         e = EVENT_PRINTER_QUEUE_REJECTING_JOBS
  1517.                     else:
  1518.                         e = EVENT_FAX_QUEUE_REJECTING_JOBS
  1519.                 
  1520.             else:
  1521.                 result = cups.accept(self.cur_printer)
  1522.                 if result:
  1523.                     if self.cur_device.device_type == DEVICE_TYPE_PRINTER:
  1524.                         e = EVENT_PRINTER_QUEUE_ACCEPTING_JOBS
  1525.                     else:
  1526.                         e = EVENT_FAX_QUEUE_ACCEPTING_JOBS
  1527.                 
  1528.             if result:
  1529.                 self.updatePrintController()
  1530.                 self.cur_device.sendEvent(e, self.cur_printer)
  1531.             else:
  1532.                 log.error('Reject/Accept jobs operation failed')
  1533.         finally:
  1534.             endWaitCursor()
  1535.  
  1536.  
  1537.     
  1538.     def SetDefaultButton_clicked(self):
  1539.         beginWaitCursor()
  1540.         
  1541.         try:
  1542.             result = cups.setDefaultPrinter(self.cur_printer.encode('utf8'))
  1543.             if not result:
  1544.                 log.error('Set default printer failed.')
  1545.             else:
  1546.                 self.updatePrintController()
  1547.                 if self.cur_device.device_type == DEVICE_TYPE_PRINTER:
  1548.                     e = EVENT_PRINTER_QUEUE_SET_AS_DEFAULT
  1549.                 else:
  1550.                     e = EVENT_FAX_QUEUE_SET_AS_DEFAULT
  1551.                 self.cur_device.sendEvent(e, self.cur_printer)
  1552.         finally:
  1553.             endWaitCursor()
  1554.  
  1555.  
  1556.     
  1557.     def cancelCheckedJobs(self):
  1558.         beginWaitCursor()
  1559.         
  1560.         try:
  1561.             item = self.JobTable.firstChild()
  1562.             while item is not None:
  1563.                 if item.isOn():
  1564.                     self.cur_device.cancelJob(item.job_id)
  1565.                 
  1566.                 item = item.nextSibling()
  1567.         finally:
  1568.             endWaitCursor()
  1569.  
  1570.         self.updatePrintControlTab()
  1571.  
  1572.     
  1573.     def closeEvent(self, event):
  1574.         self.cleanup()
  1575.         event.accept()
  1576.  
  1577.     
  1578.     def cleanup(self):
  1579.         self.cleanupChildren()
  1580.  
  1581.     
  1582.     def cleanupChildren(self):
  1583.         log.debug('Cleaning up child processes.')
  1584.         
  1585.         try:
  1586.             os.waitpid(-1, os.WNOHANG)
  1587.         except OSError:
  1588.             pass
  1589.  
  1590.  
  1591.     
  1592.     def quit(self):
  1593.         self.cleanupChildren()
  1594.         self.close()
  1595.  
  1596.     
  1597.     def PreferencesAction_activated(self, tab_to_show = 0):
  1598.         dlg = SettingsDialog(self)
  1599.         dlg.TabWidget.setCurrentIndex(tab_to_show)
  1600.         if dlg.exec_() == QDialog.Accepted:
  1601.             self.user_settings.load()
  1602.             self.cur_device.sendEvent(EVENT_USER_CONFIGURATION_CHANGED, self.cur_printer)
  1603.         
  1604.  
  1605.     
  1606.     def SetupDeviceAction_activated(self):
  1607.         if utils.which('hp-setup'):
  1608.             cmd = 'hp-setup --gui'
  1609.         else:
  1610.             cmd = 'python ./setup.py --gui'
  1611.         log.debug(cmd)
  1612.         utils.run(cmd, log_output = True, password_func = None, timeout = 1)
  1613.         self.rescanDevices()
  1614.         self.updatePrinterCombos()
  1615.  
  1616.     
  1617.     def RemoveDeviceAction_activated(self):
  1618.         if utils.which('hp-setup'):
  1619.             cmd = 'hp-setup --gui --remove'
  1620.         else:
  1621.             cmd = 'python ./setup.py --gui --remove'
  1622.         if self.cur_device_uri is not None:
  1623.             cmd += ' --device=%s' % self.cur_device_uri
  1624.         
  1625.         log.debug(cmd)
  1626.         utils.run(cmd, log_output = True, password_func = None, timeout = 1)
  1627.         self.rescanDevices()
  1628.         self.updatePrinterCombos()
  1629.  
  1630.     
  1631.     def runExternalCommand(self, cmd, macro_char = '%'):
  1632.         beginWaitCursor()
  1633.         
  1634.         try:
  1635.             if len(cmd) == 0:
  1636.                 FailureUI(self._DevMgr5__tr('<p><b>Unable to run command. No command specified.</b><p>Use <pre>Configure...</pre> to specify a command to run.'))
  1637.                 log.error('No command specified. Use settings to configure commands.')
  1638.             else:
  1639.                 log.debug('Run: %s %s (%s) %s' % ('********************', cmd, self.cur_device_uri, '********************'))
  1640.                 log.debug(cmd)
  1641.                 
  1642.                 try:
  1643.                     cmd = []([ self.cur_device.device_vars.get(x, x) for x in cmd.split(macro_char) ])
  1644.                 except AttributeError:
  1645.                     pass
  1646.  
  1647.                 log.debug(cmd)
  1648.                 path = cmd.split()[0]
  1649.                 args = cmd.split()
  1650.                 log.debug(path)
  1651.                 log.debug(args)
  1652.                 self.cleanupChildren()
  1653.                 os.spawnvp(os.P_NOWAIT, path, args)
  1654.                 qApp.processEvents()
  1655.         finally:
  1656.             endWaitCursor()
  1657.  
  1658.  
  1659.     
  1660.     def helpContents(self):
  1661.         utils.openURL(self.docs)
  1662.  
  1663.     
  1664.     def helpAbout(self):
  1665.         dlg = AboutDialog(self, prop.version, self.toolbox_version + ' (Qt4)')
  1666.         dlg.exec_()
  1667.  
  1668.     
  1669.     def __tr(self, s, c = None):
  1670.         return qApp.translate('DevMgr5', s, c)
  1671.  
  1672.  
  1673.  
  1674. class PasswordDialog(QDialog):
  1675.     
  1676.     def __init__(self, prompt, parent = None, name = None, modal = 0, fl = 0):
  1677.         QDialog.__init__(self, parent)
  1678.         self.prompt = prompt
  1679.         Layout = QGridLayout(self)
  1680.         Layout.setMargin(11)
  1681.         Layout.setSpacing(6)
  1682.         self.PromptTextLabel = QLabel(self)
  1683.         Layout.addWidget(self.PromptTextLabel, 0, 0, 1, 3)
  1684.         self.UsernameTextLabel = QLabel(self)
  1685.         Layout.addWidget(self.UsernameTextLabel, 1, 0)
  1686.         self.UsernameLineEdit = QLineEdit(self)
  1687.         self.UsernameLineEdit.setEchoMode(QLineEdit.Normal)
  1688.         Layout.addWidget(self.UsernameLineEdit, 1, 1, 1, 2)
  1689.         self.PasswordTextLabel = QLabel(self)
  1690.         Layout.addWidget(self.PasswordTextLabel, 2, 0)
  1691.         self.PasswordLineEdit = QLineEdit(self)
  1692.         self.PasswordLineEdit.setEchoMode(QLineEdit.Password)
  1693.         Layout.addWidget(self.PasswordLineEdit, 2, 1, 1, 2)
  1694.         self.OkPushButton = QPushButton(self)
  1695.         Layout.addWidget(self.OkPushButton, 3, 2)
  1696.         self.languageChange()
  1697.         self.resize(QSize(420, 163).expandedTo(self.minimumSizeHint()))
  1698.         self.connect(self.OkPushButton, SIGNAL('clicked()'), self.accept)
  1699.         self.connect(self.PasswordLineEdit, SIGNAL('returnPressed()'), self.accept)
  1700.  
  1701.     
  1702.     def getUsername(self):
  1703.         return unicode(self.UsernameLineEdit.text())
  1704.  
  1705.     
  1706.     def getPassword(self):
  1707.         return unicode(self.PasswordLineEdit.text())
  1708.  
  1709.     
  1710.     def languageChange(self):
  1711.         self.setWindowTitle(self._PasswordDialog__tr('HP Device Manager - Enter Username/Password'))
  1712.         self.PromptTextLabel.setText(self._PasswordDialog__tr(self.prompt))
  1713.         self.UsernameTextLabel.setText(self._PasswordDialog__tr('Username:'))
  1714.         self.PasswordTextLabel.setText(self._PasswordDialog__tr('Password:'))
  1715.         self.OkPushButton.setText(self._PasswordDialog__tr('OK'))
  1716.  
  1717.     
  1718.     def __tr(self, s, c = None):
  1719.         return qApp.translate('DevMgr5', s, c)
  1720.  
  1721.  
  1722.  
  1723. def showPasswordUI(prompt):
  1724.     
  1725.     try:
  1726.         dlg = PasswordDialog(prompt, None)
  1727.         if dlg.exec_() == QDialog.Accepted:
  1728.             return (dlg.getUsername(), dlg.getPassword())
  1729.     finally:
  1730.         pass
  1731.  
  1732.     return ('', '')
  1733.  
  1734.  
  1735. def openEWS(host, zc):
  1736.     if zc:
  1737.         (status, ip) = hpmudext.get_zc_ip_address(zc)
  1738.         if status != hpmudext.HPMUD_R_OK:
  1739.             ip = 'hplipopensource.com'
  1740.         
  1741.     else:
  1742.         ip = host
  1743.     return 'http://%s' % ip
  1744.  
  1745.